package com.dbh.jdk8.lambda;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * jdk1.8流理解
 * @author dongbanghui
 * @create 2022/1/13
 */
public class ArrayDemo {

    public static void main(String[] args) {
        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8});
        // 求集合元素和
        Integer result = stream.reduce(0, Integer::sum);
        System.out.println(result);

        // 求最大值
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.max(Integer::compareTo).ifPresent(System.out::println);
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce(Integer::max).ifPresent(System.out::println);

        // 做逻辑
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);

        // 求逻辑乘机
        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        result = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);
        Optional.of(result).ifPresent(System.out::println);

    }

}
